<?php
namespace Tongcheng\Controller;
use Think\Controller;
class ShopCateController extends CommonController {
    public function index(){
    	$model = M("Cate");
    	$map['wid'] = session('wid');
        $map['pid'] = 0;
        $map['type'] = "shop";
    	$list = $model->where($map)->select();
        unset($map['type']);
        foreach ($list as $key => $value) {
            $map['pid'] = $value['id'];
            $list[$key]['sub_cate'] = $model->where($map)->select();
        }
        $this->assign('list',$list);
        $this->display();
    }

    public function add(){
        if(IS_AJAX){
            $model = M("Cate");
            $new['wid'] = session('wid');
            $new['pid'] = 0;
            $new['type'] = 'shop';
            $new['title'] = I('post.title');
            $new['imgurl'] = I('post.imgurl');
            if($model->add($new)){
                $this->success('修改成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $this->display();
        }
    }

    public function edit(){
    	$model = M("Cate");
    	$map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        if(IS_AJAX){
            $new['title'] = I('post.title');
            $new['imgurl'] = I('post.imgurl');
            $model->where($map)->save($new);
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();
            $this->assign('vo',$vo);
            $this->display('add');
        }
    }
    //店铺升级主页
    public function levelup()
    {
        if(IS_AJAX){
            $model = M("level_article");
            $map['wid'] = session('wid');
            $res['count'] = $model->where($map)->count();
            $page = $_GET['page']?$_GET['page']:1;
            $limit = $_GET['limit']?$_GET['limit']:10;
            $res['list'] = $model->where($map)->order("create_time desc")->page("{$page},{$limit}")->select();
            $this->ajaxReturn($res);
        }else{
            $this->display();
        }
    }
    public function addaritcle(){
        if(IS_AJAX){
            $model = M("level_article");
            $new['wid'] = session('wid');
            $new['title'] = I('title');
            $new['content'] = I('content');
            $new['type'] = I('type');
            $new['create_time'] = date("Y-m-d H:i:s",time());
            if($model->add($new)){
                $this->success('操作成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $this->display();
        }
    }
    public function money()
    {
        $model = M("level_money");
        $map['wid'] = session('wid');
        if(IS_AJAX){
            $data['wid'] = session('wid');
            $data['gao'] = I('gao');
            $data['car'] = I('car');
            $data['jiaoyu'] = I('jiaoyu');
            $data['jiancai'] = I('jiancai');
            $data['takeout'] = I('takeout');
            $data['mini_program'] = I('mini_program');
            if($model->where($map)->find()){
                $a = $model->where($map)->save($data);
            }else{
                $data['create_time'] = date("Y-m-d H:i:s",time());
                $a = $model->add($data);
            }
            if($a){
                $this->success('操作成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $list = $model->where($map)->find();
            $this->assign('list',$list);
            $this->display();
        }

    }
    public function editaritcle(){
        $model = M("level_article");
        $map['id'] = $_GET['id'];
        if(IS_AJAX){
            $new['title'] = I('title');
            $new['content'] = I('content');
            $new['type'] = I('type');
            $model->where($map)->save($new);
            echo $model->getLastSql();exit;
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();
            $this->assign('vo',$vo);
            $this->display('addaritcle');
        }
    }
    public function delaritcle(){
        $model = M("level_article");
        $map['wid'] = session('wid');
        $map['id'] = I('id');

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }
    //店铺升级审核
    public function audit()
    {
        $list = M('level_order')->alias('o')->field('o.*, s.name, s.username, s.tel')->join('tp_shop s on s.shop_id = o.shop_id')
            ->where(['o.wid'=>session('wid'),'o.is_pay'=>1])
            ->order('o.create_time desc,is_pay asc')
            ->select();
        $this->assign('info',$list);
        $this->display();
    }
    //审核
    public function commit()
    {
        $log = M('level_order')->where(['id'=>I('id')])->find();
        $shop = M('shop')->where(['shop_id'=>$log['shop_id']])->find();
        //修改店铺模版
        if($shop){
            switch ($log['type']){
                case 'gao':
                    $type = '';
                    break;
                default:
                    $type = $log['type'];
                    break;
            }
            if ($log['type'] == 'mini_program') {
                $a = 1;
            } else {
                $a = M('shop')->where(['shop_id'=>$log['shop_id']])->save(['type'=>$type]);
            }
        }
        //修改订单信息
        $b = M('level_order')->where(['id'=>I('id')])->save(['status'=>1]);
        //$a= sendAuditMessage($log['id'],'level');
        if($a && $b){
            //操作成功，发送模版消息
            sendAuditMessage($log['id'],'level');
            $this->success('操作成功！');
        }else{
            $this->error('网络忙，请稍后再试！');
        }
    }
    public function sub_add(){
        $model = M("Cate");
        if(IS_AJAX){
            $new['wid'] = session('wid');
            $new['pid'] = $_GET['id'];
            $new['type'] = 'shop';
            $new['title'] = I('post.title');
            $new['url'] = '';
            $new['imgurl'] = I('post.imgurl');
            if($model->add($new)){
                $this->success('添加成功！');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $this->display('add');
        }
    }

    public function sub_edit(){
        $model = M("Cate");
        $map['wid'] = session('wid');
        $map['id'] = $_GET['id'];
        if(IS_AJAX){
            $new['title'] = I('post.title');
            $new['imgurl'] = I('post.imgurl');
            $model->where($map)->save($new);
            $this->success('修改成功！');
        }else{
            $vo = $model->where($map)->find();
            $this->assign('vo',$vo);
            $this->display('add');
        }
    }

    public function del(){
    	$model = M("Cate");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];

        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }
}